﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_agendapc : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Utilizador util2;
        public Cliente cli1;
        public string dt1;
        public string hr1;

        private int num;
        private int num1;
        private int num2;

        public frm_agendapc(Utilizador util21, string dt, string hr)
        {
            this.util2 = util21;
            this.dt1 = dt;
            this.hr1 = hr;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            pesquisa_todos();
        }

        private void cmdagenda_voltar_Click(object sender, EventArgs e)
        {
            frm_agendadet frm2 = new frm_agendadet(util2, cli1, null, null, "voltar", dt1, hr1, null);
            this.Close();
            frm2.ShowDialog();
        }

        private void cmdagenda_pcnome_Click(object sender, EventArgs e)
        {
            if (txtagenda_cnome.Text == "")
            {
                MessageBox.Show("Primeiro tem de inserir o nome do cliente a pesquisar", "Insira o nome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int id = util2.get_id_vet();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE nome LIKE '%" + txtagenda_cnome.Text + "%' and id_veterinario = " + id + " and oculto = 0";
                SqlDataReader readatrib = command1.ExecuteReader();

                this.dataGridView1.Rows.Clear();

                while (readatrib.Read())
                {
                    int i = this.dataGridView1.Rows.Add();

                    this.dataGridView1[0, i].Value = readatrib["id_cliente"].ToString();
                    this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                    this.dataGridView1[2, i].Value = readatrib["morada"].ToString();
                    this.dataGridView1[3, i].Value = readatrib["cod_postal"].ToString();
                    this.dataGridView1[4, i].Value = readatrib["localidade"].ToString();
                    this.dataGridView1[5, i].Value = readatrib["telemovel"].ToString();
                    this.dataGridView1[6, i].Value = readatrib["email"].ToString();

                }

                con.Close();
            }
        }

        private void cmdagenda_icnome_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                Cliente m = new Cliente(Convert.ToInt32(dataGridView1["id_cliente", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["nome", dataGridView1.CurrentRow.Index].Value.ToString(), dataGridView1["morada", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["cod_postal", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["localidade", dataGridView1.CurrentRow.Index].Value.ToString(), Convert.ToInt32(dataGridView1["telemovel", dataGridView1.CurrentRow.Index].Value.ToString()), dataGridView1["email", dataGridView1.CurrentRow.Index].Value.ToString());

                int i = this.dataGridView1.CurrentRow.Index;
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int id = util2.get_id_vet();
                string temp_cli = this.dataGridView1.Rows[i].Cells["id_cliente"].Value.ToString();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT COUNT(*) FROM Cliente c INNER JOIN Animais an ON c.id_cliente = an.id_cliente WHERE c.id_cliente = '"+ temp_cli +"' and c.id_veterinario = " + id + " and c.oculto = 0";
                num = (Int32)command1.ExecuteScalar();

                con.Close();

                if (num > 0)
                {
                    con = new System.Data.SqlClient.SqlConnection();
                    con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                    con.Open();

                    int id1 = util2.get_id_vet();
                    SqlCommand command2 = con.CreateCommand();
                    command2.CommandText = "SELECT COUNT(*) FROM Cliente c INNER JOIN Exploracao e ON c.id_cliente = e.id_cliente WHERE c.id_cliente = '" + temp_cli + "' and c.id_veterinario = " + id1 + " and c.oculto = 0";
                    num1 = (Int32)command2.ExecuteScalar();

                    con.Close();

                    if (num1 > 0)
                    {
                        frm_agendadet frm1 = new frm_agendadet(util2, m, null, null, "inscliente", dt1, hr1, "ndefinido");
                        this.Hide();
                        frm1.ShowDialog();
                    }
                    else
                    {
                        frm_agendadet frm1 = new frm_agendadet(util2, m, null, null, "inscliente", dt1, hr1, "animal");
                        this.Hide();
                        frm1.ShowDialog();
                    }
                }
                else
                {
                    con = new System.Data.SqlClient.SqlConnection();
                    con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                    con.Open();

                    int id2 = util2.get_id_vet();
                    SqlCommand command2 = con.CreateCommand();
                    command2.CommandText = "SELECT COUNT(*) FROM Cliente c INNER JOIN Exploracao e ON c.id_cliente = e.id_cliente WHERE c.id_cliente = '"+ temp_cli +"' and c.id_veterinario = " + id2 + " and c.oculto = 0";
                    num2 = (Int32)command2.ExecuteScalar();

                    con.Close();

                    if (num2 > 0)
                    {
                        frm_agendadet frm1 = new frm_agendadet(util2, m, null, null, "inscliente", dt1, hr1, "exploracao");
                        this.Hide();
                        frm1.ShowDialog();
                    }
                }
            }
        }

        private void pesquisa_todos()
        {
            try
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int id = util2.get_id_vet();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_cliente, nome, morada, cod_postal, localidade, telemovel, email FROM Cliente WHERE id_veterinario = " + id + " and oculto = 0";
                SqlDataReader readatrib = command1.ExecuteReader();

                this.dataGridView1.Rows.Clear();

                while (readatrib.Read())
                {
                    int i = this.dataGridView1.Rows.Add();

                    this.dataGridView1[0, i].Value = readatrib["id_cliente"].ToString();
                    this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                    this.dataGridView1[2, i].Value = readatrib["morada"].ToString();
                    this.dataGridView1[3, i].Value = readatrib["cod_postal"].ToString();
                    this.dataGridView1[4, i].Value = readatrib["localidade"].ToString();
                    this.dataGridView1[5, i].Value = readatrib["telemovel"].ToString();
                    this.dataGridView1[6, i].Value = readatrib["email"].ToString();

                }

                con.Close();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }
    }
}
